<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>进货管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../../lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="../../../css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
		<!--搜索开始-->
        <fieldset class="layui-elem-field layuimini-search">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="" id="searchFrm">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">供应商</label>
                            <div class="layui-input-inline">
								<select name="providerid" class="providerSel">
									<option value ="">请选择供应商</option>
								</select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">商品名称</label>
                            <div class="layui-input-inline">
								<select name="goodsid" class="goodsSel">
									<option value ="">请选择商品</option>
								</select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">开始时间</label>
                            <div class="layui-input-inline">
                                <input type="text" name="startTime" id="startTime" readonly="readonly" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">结束时间</label>
                            <div class="layui-input-inline">
                                <input type="text" name="endTime" id="endTime" readonly="readonly" autocomplete="off" class="layui-input">
                            </div>
                        </div>
					</div>
					<div class="layui-form-item">
                        <div class="layui-block" style="text-align: center;">
                            <a class="layui-btn " lay-submit=""  lay-filter="data-search-btn"><label class="layui-icon layui-icon-search"></label>搜索</a>
							<a class="layui-btn layui-btn-warm" onclick="javascript:$('#searchFrm')[0].reset()" lay-filter="data-search-btn"><label class="layui-icon layui-icon-refresh"></label>重置</a>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>
		<!--搜索结束-->
		
		<!--表格开始-->
        <table class="layui-hide" id="inportTable" lay-filter="inportTable"></table>
        <div style="display: none;" id="inportToolBar">
            <a class="layui-btn layui-btn-sm btn_add" lay-event="add">添加</a>
        </div>
		<div style="display: none;" id="inportTableBar">
			<a class="layui-btn layui-btn-sm layui-btn-warm data-count-delete btn_update" lay-event="update">修改</a>
		    <!-- <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete btn_delete" lay-event="delete">删除</a> -->
			<a class="layui-btn layui-btn-sm layui-btn-danger data-count-delete btn_back" lay-event="back">退货</a>
		</div>
		<!--表格结束-->
		
		<!--修改的弹出层开始-->
		<div id="updateDiv" style="display: none;padding: 0.3125rem">
			<form method="post" class="layui-form layui-form-pane" lay-filter="dataFrm" id="dataFrm">
				<div class="layui-form-item">
					<h3 style="text-align: center;"><em>本修改菜单旨在修改因人为失误造成的进货数量、价格等错误</em></h3>
				</div>
				 <div class="layui-form-item">
					<div class="layui-inline">
						<label class="layui-form-label">供应商</label>
						<div class="layui-input-inline">
							<input type="hidden" name="id" />
							<input type="text" name="providername" id="providername" class="layui-input" autocomplete="off" disabled="disabled" style="background: #CCCCCC;">
						</div>
					</div>
					
					<div class="layui-inline">
						<label class="layui-form-label">商品名称</label>
						<div class="layui-input-inline">
						   <input type="hidden" name="goodsid" id="update_goodsid" />
						   <input type="text" name="goodsname" id="goodsname" class="layui-input" autocomplete="off" disabled="disabled" style="background: #CCCCCC;" >
						</div>
					</div>
				  </div>
				  <div class="layui-form-item">
					  <div class="layui-inline">
						<label class="layui-form-label">进货数量</label>
						<div class="layui-input-inline">
							<input type="text" name="number" id="number" lay-verify="required|number|checkupdategoodsnumber" autocomplete="off" class="layui-input">
						</div>
					  </div>
					  <div class="layui-inline">
							<label class="layui-form-label">进货价格</label>
							<div class="layui-input-inline">
								<input type="text" name="inportprice" lay-verify="required|number" autocomplete="off" class="layui-input">
							</div>
					  </div>
					  <div class="layui-inline">
						  <label class="layui-form-label">商品在库数</label>
						  <div class="layui-input-inline">
							  <input type="text" name="numberNow" id="numberNow" lay-verify="number" autocomplete="off" class="layui-input" disabled="disabled" style="background: #ccc;">
						  </div>
					  </div>
					  <div class="layui-inline">
						<label class="layui-form-label">商品销售数</label>
						<div class="layui-input-inline">
							<input type="text" name="numberSale" id="numberSale" lay-verify="number" autocomplete="off" class="layui-input" disabled="disabled" style="background: #ccc;">
						</div>
					</div>
					<div class="layui-inline">
						<label class="layui-form-label">商品报废数</label>
						<div class="layui-input-inline">
							<input type="text" name="numberScrap" id="numberScrap" lay-verify="number" autocomplete="off" class="layui-input" disabled="disabled" style="background: #ccc;">
						</div>
					</div>
					<div class="layui-inline">
						<label class="layui-form-label">商品退货数</label>
						<div class="layui-input-inline">
							<input type="text" name="numberBack" id="numberBack" lay-verify="number" autocomplete="off" class="layui-input" disabled="disabled" style="background: #ccc;">
						</div>
					</div>
				   </div>
				   <div class="layui-form-item suggestion_number">
					   <p style="text-align: center;">当前建议的进货数量不低于：<strong id="adviseNum">100</strong></p>
				   </div>
				   <div class="layui-form-item">
						<label class="layui-form-label">支付类型</label>
						<div class="layui-input-block">
							<input type="radio" name="paytype" value="支付宝" title="支付宝" checked="">
							<input type="radio" name="paytype" value="微信" title="微信">
							<input type="radio" name="paytype" value="银联" title="银联">
						</div>
				    </div>
					<div class="layui-form-item">
							<label class="layui-form-label">备注</label>
							<div class="layui-input-block">
						      <textarea placeholder="请输入备注" name="remark" class="layui-textarea"></textarea>
							</div>
					 </div>
				  <div class="layui-form-item">
						<div class="layui-input-block" style="text-align: left;">
							<a class="layui-btn " lay-submit=""  lay-filter="doSubmit"><label class="layui-icon layui-icon-release"></label>提交</a>
							<!-- <a class="layui-btn layui-btn-warm" onclick="javascript:$('#dataFrm')[0].reset()" lay-filter="data-search-btn"><label class="layui-icon layui-icon-refresh"></label>重置</a> -->
						</div>
				   </div>
			</form>
		</div>
		<!--修改的弹出层结束-->



		<!--添加的弹出层开始-->
		<div id="addDiv" style="display: none;padding: 0.3125rem">
			<form method="post" class="layui-form layui-form-pane" lay-filter="addDataFrm" id="addDataFrm">
				<div class="layui-form-item">
					<div class="layui-inline">
						<label class="layui-form-label">供应商</label>
						<div class="layui-input-inline">
								<input type="hidden" name="id" />
								<select name="providerid" id="data_providerid" lay-filter="data_providerid" class="providerSel">
									<option value ="">请选择供应商</option>
								</select>
						</div>
					</div>
					
					<div class="layui-inline">
						<label class="layui-form-label">商品名称</label>
						<div class="layui-input-inline">
							<input type="text" name="goodsname" id="goodsname" lay-verify="required" autocomplete="off" class="layui-input">
						</div>
					</div>

					<div class="layui-inline">
						<label class="layui-form-label">产地</label>
						<div class="layui-input-inline">
							<input type="text" name="produceplace" lay-verify="required" autocomplete="off" class="layui-input">
						</div>
					</div>
				</div>


				<div class="layui-form-item">
					<div class="layui-inline">
						<label class="layui-form-label">规格</label>
						<div class="layui-input-inline">
							<input type="text" name="size" lay-verify="required" autocomplete="off" class="layui-input">
						</div>
					</div>
					
					<div class="layui-inline">
						<label class="layui-form-label">包装</label>
						<div class="layui-input-inline">
							<input type="text" name="goodspackage" lay-verify="required" autocomplete="off" class="layui-input">
						</div>
					</div>

					<div class="layui-inline">
						<label class="layui-form-label">生产批号</label>
						<div class="layui-input-inline">
							<input type="text" name="productcode" lay-verify="required" autocomplete="off" class="layui-input">
						</div>
					</div>
				</div>


				<div class="layui-form-item">
					<div class="layui-inline">
						<label class="layui-form-label">批准文号</label>
						<div class="layui-input-inline">
							<input type="text" name="promitcode" lay-verify="required" autocomplete="off" class="layui-input">
						</div>
					</div>
					
					<div class="layui-inline">
						<label class="layui-form-label">生产时间</label>
						<div class="layui-input-inline">
							<input type="text" name="productiondate" id="productiondate" readonly="readonly" lay-verify="required" autocomplete="off" class="layui-input">
						</div>
					</div>

					<div class="layui-inline">
						<label class="layui-form-label">到期时间</label>
						<div class="layui-input-inline">
							<input type="text" name="expiration" id="expiration" readonly="readonly" lay-verify="required" autocomplete="off" class="layui-input">
						</div>
					</div>
				</div>
				

				<div class="layui-form-item">
					<div class="layui-inline">
						<label class="layui-form-label">图片</label>
						<div class="layui-input-inline">
							<input type="text" name="goodsimg" lay-verify="required" autocomplete="off" class="layui-input">
						</div>
					</div>
					
					<div class="layui-inline">
						<label class="layui-form-label">商品描述</label>
						<div class="layui-input-inline">
							<input type="text" name="description" lay-verify="required" autocomplete="off" class="layui-input">
						</div>
					</div>
				</div>


				<div class="layui-form-item">
					<div class="layui-inline">
						<label class="layui-form-label">进货数量</label>
						<div class="layui-input-inline">
							<input type="text" name="number" lay-verify="required|number" autocomplete="off" class="layui-input">
						</div>
					</div>
					<div class="layui-inline">
						<label class="layui-form-label">进货价格</label>
						<div class="layui-input-inline">
							<input type="text" name="inportprice" lay-verify="required|number" autocomplete="off" class="layui-input">
						</div>
					</div>
				</div>

				<div class="layui-form-item">
					<label class="layui-form-label">进货支付类型</label>
					<div class="layui-input-block">
						<input type="radio" name="paytype" value="支付宝" title="支付宝" checked="">
						<input type="radio" name="paytype" value="微信" title="微信">
						<input type="radio" name="paytype" value="银联" title="银联">
					</div>
				</div>

				<div class="layui-form-item">
					<label class="layui-form-label">备注</label>
					<div class="layui-input-block">
						<textarea placeholder="请输入备注" name="remark" class="layui-textarea"></textarea>
					</div>
				</div>

				<div class="layui-form-item">
					<div class="layui-input-block" style="text-align: center;">
						<a class="layui-btn " lay-submit=""  lay-filter="doSubmitAdd"><label class="layui-icon layui-icon-release"></label>提交</a>
						<a class="layui-btn layui-btn-warm" onclick="javascript:$('#addDataFrm')[0].reset()" lay-filter="data-search-btn"><label class="layui-icon layui-icon-refresh"></label>重置</a>
					</div>
				</div>
			</form>
		</div>
		<!--添加的弹出层结束-->


		
		<!--退货的弹出层 开始-->
		<div id="backDiv" style="display: none;padding: 0.3125rem">
			<form method="post" class="layui-form layui-form-pane" lay-filter="backDataFrm" id="backDataFrm">
				  <div class="layui-form-item">
					  <div class="layui-form-inline">
						<label class="layui-form-label">退货数量</label>
						<div class="layui-input-inline">
							<input type="hidden" name="goodsid" id="back_goodsid" />
							<input type="hidden" name="inportid" id="inportid" />
							<input type="text" name="number" id="num_1" placeholder="请输入退货数量(必填)" onchange="changeEvent();" lay-verify="required|number|checkgoodsnumber" autocomplete="off" class="layui-input">
						</div>
						
						<div class="layui-form-inline">
							<label class="layui-form-label">当前在库数</label>
							<div class="layui-input-inline">
								<input type="text" name="numberNow"  autocomplete="off" class="layui-input" disabled="disabled" style="background: #ccc;">
							</div>
						</div>
					</div>
				   </div>


				   <div class="layui-form-item">
					<div class="layui-form-inline">
					  <label class="layui-form-label">退货价格</label>
					  <div class="layui-input-inline">
						  <input type="text" name="outportprice" id="price_1" placeholder="请输入退货价格(必填)" onchange="changeEvent();" lay-verify="required|number" autocomplete="off" class="layui-input">
					  </div>
					  
					  <div class="layui-form-inline">
						  <label class="layui-form-label">退货总价</label>
						  <div class="layui-input-inline">
							  <input type="text" name="totalbackprice" id="totalprice" autocomplete="off" class="layui-input" disabled="disabled" style="background: rgb(204, 204, 204);">
						  </div>
					  </div>
				  </div>
				 </div>

				  <div class="layui-form-item">
						<label class="layui-form-label">退货原因</label>
						<div class="layui-input-block">
						  <textarea placeholder="请输入退货原因(选填)" name="remark" class="layui-textarea"></textarea>
						</div>
				   </div>
				  <div class="layui-form-item">
						<div class="layui-input-block" style="text-align: left;">
							<a class="layui-btn " lay-submit=""  lay-filter="doBack"><label class="layui-icon layui-icon-release"></label>提交</a>
							<!-- <a class="layui-btn layui-btn-warm" onclick="javascript:$('#backDataFrm')[0].reset()" lay-filter="data-search-btn"><label class="layui-icon layui-icon-refresh"></label>重置</a> -->
						</div>
				   </div>
			</form>
		</div>
		<!--退货的弹出层 结束-->
    </div>
</div>
<script src="../../../lib/jquery-3.4.1/jquery-3.4.1.min.js" charset="utf-8"></script>
<script src="../../../lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script src="../../../js/lay-config.js?v=1.0.4" charset="utf-8"></script>
<script src="../../../lib/jq-module/jquery.particleground.min.js" charset="utf-8"></script>
<script src="../../../lib/common/jquery.cookie.min.js" charset="utf-8"></script>
<script src="../../../lib/common/common.js" charset="utf-8"></script>

<script>

	//监听input框的输入事件
	function changeEvent(){
		var num_1 = $("#num_1").val();
		var price_1 = $("#price_1").val()
		if(num_1 == null || price_1 == null || num_1 ==0 || price_1 == 0){
			$("#totalprice").val(0);
		}else{
			$("#totalprice").val(num_1*price_1)
		}
	}

    layui.use(['form', 'table','laydate','layer'], function () {
        var form = layui.form;
        var table = layui.table;
		var laydate=layui.laydate;
		var layer=layui.layer;

		//渲染时间选择器
		laydate.render({
			elem:'#startTime',
			type:'datetime'
		});
		laydate.render({
			elem:'#endTime',
			type:'datetime'
		});

		//渲染时间选择器
		laydate.render({
			elem:'#productiondate',
			type:'datetime'
		});
		laydate.render({
			elem:'#expiration',
			type:'datetime'
		});

	//加载查询条件里面的供应商的下拉列表
	$.get(api+"provider/getAllAvailableProvider",function(res){
		var search_providerid=$(".providerSel");
		var html="<option value =''>请选择供应商</option>";
		var data=res.data;
		$.each(data,function(index,item){
			html+="<option value ="+item.id+">"+item.providername+"</option>"
		});
		search_providerid.html(html);
		form.render("select");
	});
	//加载查询条件里面的商品的下拉列表
	$.get(api+"goods/getAllAvailableGoods",function(res){
		var search_goodsid=$(".goodsSel");
		var html="<option value =''>请选择商品</option>";
		var data=res.data;
		$.each(data,function(index,item){
			html+="<option value ="+item.id+">"+item.goodsname+"["+item.size+"]"+"</option>"
		});
		search_goodsid.html(html);
		form.render("select");
	});
	

      var tableIns=table.render({
            elem: '#inportTable',
            url: api+'inport/loadAllInport',
			toolbar:"#inportToolBar",
			cellMinWidth:true,
            cols: [[
                {field: 'id', title: 'ID', align: "center",width:80},
                {field: 'providername',  title: '供应商', align: "center",width:150},
                {field: 'goodsname',  title: '商品名称', align: "center",width:150},
                {field: 'size',  title: '规格', align: "center",width:150},
                {field: 'inporttime',  title: '进货时间', align: "center",width:180},
                {field: 'operateperson',  title: '操作人', align: "center",width:120},
                {field: 'number',  title: '进货数量', align: "center",width:120},
				{field: 'numberNow',  title: '当前库存', align: "center",width:120},
                {field: 'inportprice',  title: '进货价格', align: "center",width:120},
                {field: 'paytype',  title: '支付类型', align: "center",width:120},
                {field: 'remark',  title: '备注', align: "center",width:150},
                {title: '操作', templet: '#inportTableBar', fixed: "right", align: "center",width:200}
            ]],
            page: true
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
			var params= $("#searchFrm").serialize();
            //执行搜索重载
            table.reload('inportTable', {
                page: {
                    curr: 1
                }
                ,url:api+'inport/loadAllInport?'+params
            });
            return false;
        });

		//监听表头的事件
		table.on('toolbar(inportTable)', function(obj){
		  var checkStatus = table.checkStatus(obj.config.id);
		  switch(obj.event){
			case 'add':
				openAddInport();
			break;
		  };
		});

		
		//监听行数据的事件
        table.on('tool(inportTable)', function (obj) {
            var data = obj.data;
            if (obj.event === 'delete') {
                layer.confirm('真的删除这条进货数据么', function (index) {
					$.post(api+"inport/deleteInport",{id:data.id},function(res){
						layer.msg(res.msg);
						obj.del();
						layer.close(index);
					})
                });
            }else if(obj.event === 'update'){
				$.ajaxSettings.async = false;
				$.get(api+"goods/getwhetherAvailableGoods",{goodsid:data.goodsid},function(res0){
					if(res0.code == 200){
						openUpdateInport(data);
					}else{
						layer.confirm(res0.msg+",请联系商品销售经理进行商品下架后再执行后续操作！",function(index){
							layer.close(index);
						})
					}
				});
				$.ajaxSettings.async = true;
		    }else if(obj.event === 'back'){
				$.get(api+"goods/getwhetherAvailableGoods",{goodsid:data.goodsid},function(res0){
					if(res0.code == 200){
						openBack(data);
					}else{
						layer.confirm(res0.msg+",请联系商品销售经理进行商品下架后再执行后续操作！",function(index){
							layer.close(index);
						})
					}
				});
			}
        });
		
		//打开添加进货的弹层
		var mainIndex;
		var url;  //动态的url
		function openAddInport(){
			mainIndex=layer.open({
				type:1,
				title:'添加进货',
				content:$("#addDiv"),
				area:['800px','600px'],
				success:function(index){
					url=api+"inport/addInport";
					//重置表单
					$("#addDataFrm")[0].reset();
					
				}
			});	
		}


		function openUpdateInport(data){
			mainIndex=layer.open({
				type:1,
				title:'修改['+data.goodsname+']进货',
				content:$("#updateDiv"),
				area:['800px','600px'],
				success:function(index){
					url=api+"inport/updateInport";
					form.val("dataFrm",data);
					$("#adviseNum").text(data.numberSale+data.numberScrap+data.numberBack);
					// form.render();
				}
			});	
		}


		//提交数据|修改
		form.on("submit(doSubmit)",function(data){
			if(Number($("#numberScrap").val())+Number($("#numberSale").val())+Number($("#numberBack").val())>Number($("#number").val())){
				layer.msg("当前修改进货数不可少于已经消耗的商品数！");
			}else{
				
				var params= $("#dataFrm").serialize();
				params+="&numberSale="+$("#numberSale").val()+"&numberScrap="+$("#numberScrap").val()+"&numberBack="+$("#numberBack").val();
				$.post(url,params, function(res){
					layer.msg(res.msg);
					if(res.code==200){
						tableIns.reload({
							url: api+'inport/loadAllInport',
						});
					}
					layer.close(mainIndex);
				})
			}
			return false;
		});

		//提交数据|增加
		form.on("submit(doSubmitAdd)",function(data){
			var params= $("#addDataFrm").serialize();
			$.post(url,params, function(res){
				layer.msg(res.msg);
				if(res.code==200){
					tableIns.reload();
				}
				layer.close(mainIndex);
			})
			return false;
		});
		
		//打开退货的弹出层
		function openBack(data){
			mainIndex=layer.open({
				type:1,
				title:"【"+data.goodsname+'】的退货',
				content:$("#backDiv"),
				area:['700px','400px'],
				success:function(index){
					//重置表单
					$("#backDataFrm")[0].reset();
					// console.log(data)
					form.val("backDataFrm",data);
					form.val("backDataFrm",{number:'',inportid:data.id,remark:''});
				}
			});	
		}
		
		//提交数据
		form.on("submit(doBack)",function(data){
			var params= $("#backDataFrm").serialize();
			$.post(api+"outport/addOutport",params, function(res){
				layer.msg(res.msg);
				if(res.code==200){
					tableIns.reload();
				}
				layer.close(mainIndex);
			})
			return false;
		});
		
        //自定义校验方式
		form.verify({
			//退货校验
			checkgoodsnumber: function(value, item){ //value：表单的值、item：表单的DOM对象
		    var str;
			var goodsid0=$("#back_goodsid").val();
			$.ajax({
				url:api+"goods/getGoodsByGoodId?goodsid="+goodsid0,
				type:'get',
				async:false,
				success:function(res){
					if(res.data.number<value){
						str="退货数量不能大于当前商品库存";
					}else if(value<=0){
						str="退货数量不能小于1";
					}
				}
			})
			return str;
		  	},

		  	//修改校验
		  	checkupdategoodsnumber: function(value, item){ //value：表单的值、item：表单的DOM对象
				var str;
				var goodsid1=$("#update_goodsid").val();
				if(value < 0 ){
					str = "修改数量不能小于0！"
				}
				return str;
		 	}
		  
		});   

    });
</script>
<script>

</script>

</body>
</html>